import netsnmp

community = 'public'
version = 2

HOSTNAME   = '.1.3.6.1.2.1.1.5.0'
def get_hostname(host, community='public', version=2):

    s = netsnmp.Session(DestHost=host, Version=version, Community=community)
    vars = netsnmp.VarList(netsnmp.Varbind(HOSTNAME))

    hostname = s.get(vars)
    if hostname[0] == None:
        return "None"
    return hostname[0]


def index(req):
    req.add_common_vars()
    env_vars = req.subprocess_env
    getReqStr = env_vars['QUERY_STRING']
    getReqArr = getReqStr.split('&')   
    getReqDict = {}

    for item in getReqArr:   
        tempArr = item.split('=') 
        getReqDict[tempArr[0]] = tempArr[1]

    req.content_type = 'text/javascript'
    
    status = '200 OK'
   
    # Parse parameter for snmp connection (community, version) 
    if getReqDict.has_key('community'):
        community = getReqDict['community']
    if getReqDict.has_key('version'):
        version = int(getReqDict['version'])
    
    output = get_hostname(getReqDict['host'], community, version)
    return output

if __name__ == "__main__":
    host = raw_input("host IP> ")
    print get_hostname(host)

